#include "KmyIScene.h"
#include "KmySceneManager.h"

KmyIScene::KmyIScene()
{
    scene_manager = NULL;
}

KmyIScene::KmyIScene(KmySceneManager *manager,
                     int screen_count, KmyICanvas **screens){
    scene_manager = manager;
    this->screen_count = screen_count;
    this->screens = screens;

    ui_layers = new KmyNodeLayer*[screen_count];
    world_layers = new KmyWorld*[screen_count];
    for (int i=0; i<screen_count; i++){
        ui_layers[i] = new KmyNodeLayer();
        world_layers[i] = new KmyWorld(screens[i]->WIDTH, screens[i]->HEIGHT);
    }
}

KmyIScene::~KmyIScene(){

}

void KmyIScene::finalize(){
    scene_manager = NULL;
    for (int i=0; i<screen_count; i++){

        if (ui_layers[i]!=NULL){
            delete ui_layers[i];
        }
        if (world_layers[i]!=NULL){
            delete world_layers[i];
        }
    }
    delete[] world_layers;
    delete[] ui_layers;
}

KmyNodeLayer* KmyIScene::getUILayer(int screen_no){
    if (screen_no>=screen_count){
        return NULL;
    }
    return ui_layers[screen_no];
}

KmyWorld* KmyIScene::getWorld(int screen_no){
    if (screen_no>=screen_count){
        return NULL;
    }
    return world_layers[screen_no];
}

void KmyIScene::registNode(KmyNode *node){
    if (node->id == -1){
        node->id = KmyNode::createID();
    }
    map<int, KmyNode*>::iterator iter = node_map.find(node->id);
    if (iter!=node_map.end()){
        return;
    }
    node_map.insert(pair<int, KmyNode*>(node->id, node));
}

KmyNode* KmyIScene::getNode(int id){
    map<int, KmyNode*>::iterator iter = node_map.find(id);
    if (iter!=node_map.end()){
        KmyNode* p = iter->second;
        return p;
    }
    return NULL;
}

void KmyIScene::removeNode(int id){
    map<int, KmyNode*>::iterator iter = node_map.find(id);
    if (iter!=node_map.end()){
        KmyNode* p = iter->second;
        if (p!=NULL && p->parent!=NULL){
            p->parent->removeChild(p);
        }
    }
}

void KmyIScene::removeNode(KmyNode *node){
    if (node!=NULL && node->parent!=NULL){
        node->parent->removeChild(node);
    }
}

void KmyIScene::logic(){
    for (int i=0; i<screen_count; i++){
        if (ui_layers[i]!=NULL){
            ui_layers[i]->logic();
        }
        if (world_layers[i]!=NULL){
            world_layers[i]->logic();
        }
    }
}

void KmyIScene::render(){
    for (int i=0; i<screen_count; i++){
        if (ui_layers[i]!=NULL){
            ui_layers[i]->render(screens[i]);
        }
        if (world_layers[i]!=NULL){
            world_layers[i]->render(screens[i]);
        }
        screens[i]->is_need_repaint = true;
    }
}

//void KmyIScene::removeNode(int id){
//    map<int, KmyNode*>::iterator iter = node_map.find(id);
//    if (iter!=node_map.end()){
//        KmyNode* p = iter->second;
//        int nodetype = p->getNodeType();
//        switch (nodetype){
//        case KmyNode::NODE_TYPE_IMAGE:
//            removeUI(p);
//            break;
//        case KmyNode::NODE_TYPE_SPRITE:
//            removeSprite((KmySprite*)p);
//            break;
//        }
//    }
//}

//void KmyIScene::addUI(KmyNode *ui, int screen_no){
//    map<int, KmyNode*>::iterator iter = node_map.find(ui->id);
//    if (iter!=node_map.end()){
//        return;
//    }
//    if (screen_no!=0){
//        return;
//    }
//    ui_layers[0]->addChild(ui);
//    node_map.insert(pair<int, KmyNode*>(ui->id, ui));
//}

//void KmyIScene::removeUI(KmyNode *ui){
//    ui_layers[0]->removeChild(ui);
//    node_map.erase(ui->id);
//}

//void KmyIScene::addSpriteUI(KmySprite *spt, int screen_no){
//    map<int, KmyNode*>::iterator iter = node_map.find(spt->id);
//    if (iter!=node_map.end()){
//        return;
//    }
//    if (screen_no!=0){
//        return;
//    }
//    ui_layers[0]->addChild(spt);
//    node_map.insert(pair<int, KmyNode*>(spt->id, spt));
//}

//void KmyIScene::addSpriteWorld(KmySprite *spt, int map_index, int screen_no){
//    map<int, KmyNode*>::iterator iter = node_map.find(spt->id);
//    if (iter!=node_map.end()){
//        return;
//    }
//    if (screen_no!=0){
//        return;
//    }
//    KmyMap* map = world_layers[0]->getMap(map_index);
//    if (map!=NULL){
//        map->addSprite(spt);
//        node_map.insert(pair<int, KmyNode*>(spt->id, spt));
//    }
//}

//void KmyIScene::removeSprite(KmySprite *spt){
//    if (spt->parent==NULL){
//        int pt = spt->parent->getNodeType();
//        if (pt==KmyNode::NODE_TYPE_MAP){
//            KmyMap* map = (KmyMap*)(spt->parent);
//            map->removeSprite(spt);
//        }else if (pt == KmyNode::NODE_TYPE_LAYER){
//            KmyNodeLayer* layer = (KmyNodeLayer*)(spt->parent);
//            layer->removeChild(spt);
//        }else{
//            spt->parent->removeChild(spt);
//        }
//        spt->parent = NULL;
//        node_map.erase(spt->id);
//    }
//}
